package com.hilaver.dzmis.service.impl;

import java.util.List;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiAccessoryOthers;
import com.hilaver.dzmis.codetable.CtAccessoryType;
import com.hilaver.dzmis.codetable.CtApplicationType;
import com.hilaver.dzmis.codetable.CtCareLabelMaterial;
import com.hilaver.dzmis.codetable.CtMaterialType;
import com.hilaver.dzmis.codetable.CtSupplyType;
import com.hilaver.dzmis.codetable.CtUnit;
import com.hilaver.dzmis.codetable.CtYesno;
import com.hilaver.dzmis.dao.BiAccessoryOthersDAO;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.util.SimpleObj2XML;
import com.hilaver.dzmis.util.StringUtils;

public class BiAccessoryOthersServiceImpl extends AbstractBaseService {

	public final static String COLUMN_ACCESSORY_TYPE = "ctAccessoryType";

	public final static String COLUMN_MATERIAL_TYPE = "ctMaterialType";

	public final static String COLUMN_BRAND = "ctBrand";

	public final static String COLUMN_IS_SPECIAL = "isSpecial";

	public final static String COLUMN_IS_PURCHASE = "isPurchase";

	public final static String COLUMN_REFERENCE = "reference";
	
	public final static String COLUMN_BIND_PHOTO_NAME = "bindPhotoName";

	private CtServiceImpl ctService;
	
	private BiAccessoryOthersDAO dao;
	
	public BiAccessoryOthersServiceImpl(){
		this.ctService = new CtServiceImpl();
		this.dao = new BiAccessoryOthersDAO();
	}

	@Override
	public String delete(int id) throws Exception {
		super.editSysMbox(BiAccessoryOthers.class.getName(), id);
		return super.delete(BiAccessoryOthers.class.getName(), id);
	}

	public String edit(BiAccessoryOthers biAO) throws Exception {
		String operation = null;
		if (biAO.getId() != null) {
			BiAccessoryOthers old = (BiAccessoryOthers) this.dao.get(
					BiAccessoryOthers.class.getName(), biAO.getId());
			if(biAO.getPhoto1Name() == null){
				biAO.setPhoto1(old.getPhoto1());
				biAO.setPhoto1Name(old.getPhoto1Name());
			}
			if(biAO.getPhoto2Name() == null){
				biAO.setPhoto2(old.getPhoto2());
				biAO.setPhoto2Name(old.getPhoto2Name());
			}
			this.dao.getSession().evict(old);
			operation = "02";
		}else{
			operation = "01";
		}
		this.dao.saveOrUpdate(biAO);
		super.editSysMbox(biAO, operation);
		return "{success: true}";
	}

	@Override
	public String get(int id) throws Exception {
		BiAccessoryOthers biAO = (BiAccessoryOthers) this.dao.get(BiAccessoryOthers.class.getName(), id);
		
		writeFile(realFileDir + biAO.getPhoto1Name(), biAO.getPhoto1());
		writeFile(realFileDir + biAO.getPhoto2Name(), biAO.getPhoto2());
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		
		if(biAO.getBiFranceCustomer() != null){
			xmlSB.append("<" + COLUMN_CUSTOMER_ID + ">");
			xmlSB.append(biAO.getBiFranceCustomer().getId());
			xmlSB.append("</" + COLUMN_CUSTOMER_ID + ">");
			
			xmlSB.append("<customerName>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAO.getBiFranceCustomer().getName());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</customerName>");
			
			xmlSB.append("<customerDepartment>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAO.getBiFranceCustomer().getDepartment()==null?"":biAO.getBiFranceCustomer().getDepartment());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</customerDepartment>");
		}
		
		if(biAO.getBiFactory() != null){
			xmlSB.append("<" + COLUMN_FACTORY_ID + ">");
			xmlSB.append(biAO.getBiFactory().getId());
			xmlSB.append("</" + COLUMN_FACTORY_ID + ">");
			
			xmlSB.append("<factoryNameCN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAO.getBiFactory().getNameCN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</factoryNameCN>");
			
			xmlSB.append("<factoryNameEN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAO.getBiFactory().getNameEN()==null?"":biAO.getBiFactory().getNameEN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</factoryNameEN>");
			
			xmlSB.append("<addressFactoryCN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAO.getBiFactory().getAddressFactoryCN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</addressFactoryCN>");
			
			xmlSB.append("<addressFactoryEN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAO.getBiFactory().getAddressFactoryEN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</addressFactoryEN>");
		}
		
		xmlSB.append("<photo1Name>");
		if(biAO.getPhoto1Name() == null || "".equals(biAO.getPhoto1Name())){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			writeFile(realFileDir + biAO.getPhoto1Name(), biAO.getPhoto1());
			xmlSB.append(biAO.getPhoto1Name());
		}
		xmlSB.append("</photo1Name>");
		
		xmlSB.append("<photo2Name>");
		if(biAO.getPhoto2Name() == null || "".equals(biAO.getPhoto2Name())){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			writeFile(realFileDir + biAO.getPhoto2Name(), biAO.getPhoto2());
			xmlSB.append(biAO.getPhoto2Name());
		}
		xmlSB.append("</photo2Name>");
		
		xmlSB.append(SimpleObj2XML.toXMLFragment(biAO));
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	public String getWithDescription(int id) throws Exception{
		BiAccessoryOthers biAO = (BiAccessoryOthers) this.dao.get(BiAccessoryOthers.class.getName(), id);
		
		writeFile(realFileDir + biAO.getPhoto1Name(), biAO.getPhoto1());
		writeFile(realFileDir + biAO.getPhoto2Name(), biAO.getPhoto2());
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		
		xmlSB.append(this.toXMLFragment((BiAccessoryOthers) biAO));
		
		xmlSB.append(SimpleObj2XML.toXMLFragment(biAO));
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		List objList = this.dao.getAllPagination(BiAccessoryOthers.class
				.getName(), offset, Constants.PAGE_SIZE, sort, order);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (Object obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);

			xmlSB.append(this.toXMLFragment((BiAccessoryOthers) obj));

			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(BiAccessoryOthers.class.getName()));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getAllPagination(int page, String sort, String order, String[] filters)
	throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		List objList = this.dao.getAllPagination(BiAccessoryOthers.class
				.getName(), offset, Constants.PAGE_SIZE, sort, order, filters);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (Object obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);

			xmlSB.append(this.toXMLFragment((BiAccessoryOthers) obj));

			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(BiAccessoryOthers.class.getName(),filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	public String toXMLFragment(BiAccessoryOthers biAO) throws Exception {
		writeFile(realFileDir + biAO.getPhoto1Name(), biAO.getPhoto1());
		writeFile(realFileDir + biAO.getPhoto2Name(), biAO.getPhoto2());
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append("<" + COLUMN_ID + ">");
		xmlSB.append(biAO.getId());
		xmlSB.append("</" + COLUMN_ID + ">");

		xmlSB.append("<applicationReference>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(biAO.getApplicationReference()==null?"":biAO.getApplicationReference());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</applicationReference>");
		
		xmlSB.append("<accessoryReference>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(biAO.getAccessoryReference()==null?"":biAO.getAccessoryReference());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</accessoryReference>");
		
		xmlSB.append("<" + COLUMN_ACCESSORY_TYPE + ">");
		xmlSB.append(this.ctService.getDescription(CtAccessoryType.class
				.getName(), biAO.getCtAccessoryType(), this.locale));
		xmlSB.append("</" + COLUMN_ACCESSORY_TYPE + ">");
		
		xmlSB.append("<ctAccessoryTypeCode>");
		xmlSB.append(biAO.getCtAccessoryType());
		xmlSB.append("</ctAccessoryTypeCode>");
		
		xmlSB.append("<ctApplicationType>");
		xmlSB.append(this.ctService.getDescription(CtApplicationType.class
				.getName(), biAO.getCtApplicationType(), this.locale));
		xmlSB.append("</ctApplicationType>");
		
		xmlSB.append("<" + COLUMN_MATERIAL_TYPE + ">");
		xmlSB.append(this.ctService.getDescription(CtMaterialType.class
				.getName(), biAO.getCtMaterialType(), this.locale));
		xmlSB.append("</" + COLUMN_MATERIAL_TYPE + ">");
		
		xmlSB.append("<isApplication>");
		xmlSB.append(this.ctService.getDescription(CtYesno.class
				.getName(), biAO.getIsApplication(), this.locale));
		xmlSB.append("</isApplication>");
		
		xmlSB.append("<ctSupplyType>");
		xmlSB.append(this.ctService.getDescription(CtSupplyType.class
				.getName(), biAO.getCtSupplyType(), this.locale));
		xmlSB.append("</ctSupplyType>");
		
		xmlSB.append("<ctSupplyTypeCode>");
		xmlSB.append(biAO.getCtSupplyType());
		xmlSB.append("</ctSupplyTypeCode>");
		
		xmlSB.append("<ctUnit>");
		xmlSB.append(this.ctService.getDescription(CtUnit.class
				.getName(), biAO.getCtUnit(), this.locale));
		xmlSB.append("</ctUnit>");
		
		xmlSB.append("<ctUnitCode>");
		xmlSB.append(biAO.getCtUnit());
		xmlSB.append("</ctUnitCode>");
		
		xmlSB.append("<ctCareLabelMaterial1>");
		xmlSB.append(this.ctService.getDescription(CtCareLabelMaterial.class
				.getName(), biAO.getCtCareLabelMaterial1(), this.locale));
		xmlSB.append("</ctCareLabelMaterial1>");
		xmlSB.append("<ctCareLabelMaterial2>");
		xmlSB.append(this.ctService.getDescription(CtCareLabelMaterial.class
				.getName(), biAO.getCtCareLabelMaterial2(), this.locale));
		xmlSB.append("</ctCareLabelMaterial2>");
		xmlSB.append("<ctCareLabelMaterial3>");
		xmlSB.append(this.ctService.getDescription(CtCareLabelMaterial.class
				.getName(), biAO.getCtCareLabelMaterial3(), this.locale));
		xmlSB.append("</ctCareLabelMaterial3>");
		xmlSB.append("<ctCareLabelMaterial4>");
		xmlSB.append(this.ctService.getDescription(CtCareLabelMaterial.class
				.getName(), biAO.getCtCareLabelMaterial4(), this.locale));
		xmlSB.append("</ctCareLabelMaterial4>");
		xmlSB.append("<ctCareLabelMaterial5>");
		xmlSB.append(this.ctService.getDescription(CtCareLabelMaterial.class
				.getName(), biAO.getCtCareLabelMaterial5(), this.locale));
		xmlSB.append("</ctCareLabelMaterial5>");
		
		if(biAO.getBiFranceCustomer() != null){
			xmlSB.append("<" + COLUMN_CUSTOMER_NAME + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAO.getBiFranceCustomer().getName());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_CUSTOMER_NAME + ">");
			xmlSB.append("<" + COLUMN_CUSTOEMR_DEPARTMENT + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAO.getBiFranceCustomer().getDepartment()==null?"":biAO.getBiFranceCustomer().getDepartment());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_CUSTOEMR_DEPARTMENT + ">");
		}
		
		if(biAO.getBiFactory() != null){
			xmlSB.append("<factoryNameCN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAO.getBiFactory().getNameCN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</factoryNameCN>");
			
			xmlSB.append("<factoryNameEN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAO.getBiFactory().getNameEN()==null?"":biAO.getBiFactory().getNameEN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</factoryNameEN>");
			
			xmlSB.append("<addressFactoryCN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAO.getBiFactory().getAddressFactoryCN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</addressFactoryCN>");
			
			xmlSB.append("<addressFactoryEN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAO.getBiFactory().getAddressFactoryEN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</addressFactoryEN>");
		}
		
		xmlSB.append("<photo1Name>");
		if(biAO.getPhoto1Name() == null || "".equals(biAO.getPhoto1Name())){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			writeFile(realFileDir + biAO.getPhoto1Name(), biAO.getPhoto1());
			xmlSB.append(biAO.getPhoto1Name());
		}
		xmlSB.append("</photo1Name>");
		
		xmlSB.append("<photo2Name>");
		if(biAO.getPhoto2Name() == null || "".equals(biAO.getPhoto2Name())){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			writeFile(realFileDir + biAO.getPhoto2Name(), biAO.getPhoto2());
			xmlSB.append(biAO.getPhoto2Name());
		}
		xmlSB.append("</photo2Name>");

		xmlSB.append(SimpleObj2XML.toXMLFragment(biAO));
		
		return xmlSB.toString();
	}
	
	public String getMaxApplicationReferenceIndex() throws Exception{
		List<Integer> referenceIndexList = this.dao.getMaxApplicationReferenceIndex();
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append(Constants.XML_CDATA[0]);
		if(referenceIndexList.size() == 0 || referenceIndexList.get(0) == null){
			xmlSB.append("0001");
		}else{
			Integer index = referenceIndexList.get(0);
			xmlSB.append(StringUtils.to4Digital(++index));
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getMaxAccessoryReferenceIndex(String ctAccessoryType) throws Exception{
		List<Integer> referenceIndexList = this.dao.getMaxAccessoryReferenceIndex(ctAccessoryType);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append(Constants.XML_CDATA[0]);
		if(referenceIndexList.size() == 0 || referenceIndexList.get(0) == null){
			xmlSB.append("0001");
		}else{
			Integer index = referenceIndexList.get(0);
			xmlSB.append(StringUtils.to4Digital(++index));
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

}
